package com.buobao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buobao.entity.Book;
import com.buobao.mapper.BookMapper;
import com.buobao.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static org.springframework.transaction.annotation.Propagation.NOT_SUPPORTED;

@Service("bookService")
@Transactional(propagation = NOT_SUPPORTED, readOnly = true)
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;
    @Override
    public IPage<Book> paging(Long categoryId,String order,Integer page, Integer rows) {
        Page<Book> pagger = new Page<Book>(page, rows);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (categoryId>=0) {
            queryWrapper.eq("category_id", categoryId);
        }
        if ("quantity".equals(order)) {
            queryWrapper.orderByDesc("evaluation_quantity");
        } else {
            queryWrapper.orderByDesc("evaluation_score");
        }

        Page<Book> bookPage = bookMapper.selectPage(pagger, queryWrapper);
        return bookPage;
    }

    @Override
    public Book selectBookById(Long bookId) {
        return bookMapper.selectById(bookId);
    }

    @Transactional
    @Override
    public void updateEvaluation() {
        bookMapper.updateEvaluation();
    }

    @Transactional
    @Override
    public Book insert(Book book) {
        bookMapper.insert(book);
        return book;
    }
}
